Skip to content

fix: use context managers for all SQLite database connections#633

Merged
eccentriccoder01 merged 1 commit intoeccentriccoder01:mainfrom
satyakiabhijit:fix/database-context-managers
Jan 24, 2026
Merged

fix: use context managers for all SQLite database connections#633
eccentriccoder01 merged 1 commit intoeccentriccoder01:mainfrom
satyakiabhijit:fix/database-context-managers

Conversation

@satyakiabhijit
Copy link
Contributor

🔗 Related Issue

🎯 Rationale

Database connections were not using Python context managers, which can lead to connection leaks if exceptions occur before conn.close() is called. This refactoring ensures all connections are properly closed automatically.

📝 Summary of Changes

Refactored 24 SQLite database connections across 5 files to use Python context managers (with statement).

File Connections Fixed
auth/auth_utils.py 7
core/utils.py 8
pages/Journaling.py 5
setup_database.py 3
migrate_db.py 1

🔧 Technical Details

Changed from manual conn.close() calls to using with sqlite3.connect() as conn: pattern. This follows Python best practices (PEP 343) and ensures connections are automatically closed even when exceptions occur.

✅ Testing

  • All files pass Python syntax validation
  • No functionality changes - pure refactoring

📋 Checklist

  • Code follows project style guidelines
  • Self-review completed
  • No breaking changes

- Refactored 24 database connections across 5 files

- Prevents connection leaks if exceptions occur

- Follows Python best practices (PEP 343)
@eccentriccoder01
Copy link
Owner

Great Work @satyakiabhijit! Really sorry for the delayed response, I was totally caught up with exams and a bunch of other stuff... I've merged your PR, feel free to work on more issues or create new ones. Thanks for improving TalkHeal💙

@eccentriccoder01 eccentriccoder01 merged commit 1ca4e11 into eccentriccoder01:main Jan 24, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security/Reliability] Use context managers for all SQLite database connections

2 participants